﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Scoreboard.Interfaces;
using Scoreboard.Data;
using Scoreboard.Basement;
using Scoreboard.Basement.Views;
using Microsoft.Practices.Composite.Presentation.Commands;
using System.ServiceModel.DomainServices.Client;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using Microsoft.Practices.Composite.Events;
using Scoreboard.Basement.Events;
using Scoreboard.Model.Authentication;
using Scoreboard.Basement.ViewModels;

namespace Scoreboard.Main
{
	public class ShellViewModel : ViewModelBase
	{
		private PageInfo _selectedPage;
		private PageInfo _managementPage;

		public ShellViewModel()
		{
			Pages = new ObservableCollection<PageInfo>();
			Dialogs = new ObservableCollection<DialogInfo>();

			RegisterCommand = new DelegateCommand<object>(RegisterCommandHandler);

			var eventAggregator = ServiceLocator.Get<IEventAggregator>();
			var userAuthenticatedEvent = eventAggregator.GetEvent<UserAuthenticatedEvent>();
			userAuthenticatedEvent.Subscribe(UserAuthenticatedHandler);
		}

		public ObservableCollection<PageInfo> Pages { get; private set; }
		public ObservableCollection<DialogInfo> Dialogs { get; private set; }

		public ICommand RegisterCommand { get; private set; }

		public PageInfo SelectedPage
		{
			get
			{
				return _selectedPage;
			}
			set
			{
				_selectedPage = value;
				FirePropertyChanged("SelectedPage");
			}
		}

		internal void AddPage(PageBase page)
		{
			var pageInfo = new PageInfo(page);

			if (pageInfo.Title == "Управление турнирами")
			{
				_managementPage = pageInfo;
				if (WebContext.Current.IsUserRegistered
					&& WebContext.Current.User.IsInRole("Manager"))
				{
					Pages.Add(pageInfo);
				}
				return;
			}

			Pages.Add(pageInfo);

			if (page.Title == "Рейтинг")
			{
				SelectedPage = pageInfo;
			}
		}

		internal void AddDialog(DialogBase dialog)
		{
			Dialogs.Add(new DialogInfo(dialog));
		}

		public void UserAuthenticatedHandler(bool param)
		{
			if (WebContext.Current.User.IsInRole("Manager") && _managementPage != null)
			{
				Pages.Add(_managementPage);
			}
		}

		private void RegisterCommandHandler(object parameter)
		{
			var dialogService = ServiceLocator.Get<IDialogService>();
			dialogService.ShowMessage("Регистрация", "К сожалению, данные о вас не были найдены в нашем хранилище. Обратитесь пожалуйста к создателю приложения.");
		}

		private void RegisterCallback(InvokeOperation operation)
		{
			WebContext.Current.LoadUser();
		}
	}

	public class DialogInfo
	{
		public DialogInfo(DialogBase dialog)
		{
			Content = dialog;

			ShowCommand = new DelegateCommand<object>(ShowCommandHandler);
		}

		public DialogBase Content { get; private set; }

		public string Title
		{
			get
			{
				return Content.ViewModel.InitialTitle;
			}
		}		

		public ICommand ShowCommand { get; private set; }

		private void ShowCommandHandler(object parameter)
		{
			var dialogService = ServiceLocator.Get<IDialogService>();

			var parameters = new DialogParameters
			{
				Dialog = Content,
				NeedsCaching = true
			};

			dialogService.ShowDialog(parameters);
		}
	}

	public class PageInfo
	{
		public PageInfo(PageBase page)
		{
			Content = page;		
		}		

		public PageBase Content { get; private set; }

		public string Title
		{
			get
			{
				return Content.Title;
			}
		}

		public ObservableCollection<UserCommand> Commands
		{
			get
			{
				if (Content.ViewModel == null)
				{
					return null;
				}
				return Content.ViewModel.Commands;
			}
		}		
	}
}
